package com.test2;

import com.sun.org.apache.bcel.internal.classfile.Code;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

public class GenerateImage {
    /**
     * 热敏打印机纸张大约宽度（4cm*6cm）
     */
    private static final int width = 300;
    /**
     * 热敏打印机纸张大约高度（4cm*6cm）
     */
    private static final int height = 171;


    /**
     * @param code   编码
     * @param qrPath 二维码存放路劲
     * @return 最新图片生成路径
     */
    public static String ImageBuilder(String cardPath, String qrPath, String code,
                                      String name, String productAddress) {
        //得到图片缓冲区
        FileInputStream fileInputStream = null;
        try {
            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            File file = new File(qrPath);
            BufferedImage c = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            File file2 = new File("D:\\work\\html\\logo.jpg");
            fileInputStream = new FileInputStream(file2);
            BufferedImage image3 = ImageIO.read(fileInputStream);

            if (!file.exists()) {
                throw new IllegalArgumentException("二维码存放地址有误");
            }
            fileInputStream = new FileInputStream(file);
            BufferedImage image2 = ImageIO.read(fileInputStream);

            //得到它的绘制环境(这张图片的笔)
            Graphics2D g2 = (Graphics2D) bi.getGraphics();
            g2.fillRect(0, 0, width, height);
            //设置颜色
            g2.setColor(Color.WHITE);
            // 将图片大小设置为大约4cm*4cm 具体根据纸张大小设定
            g2.drawImage(image3, 260, 10, 25, 25, null);
            g2.drawImage(image2, 12, 50, 125, 125, null);
            g2.drawRect(0, 0, width - 1, height - 1);
            //设置字体:字体、字号、大小

            g2.setFont(new Font("黑体", Font.BOLD, 18));
            //设置背景颜色
            g2.setColor(Color.BLACK);
            //向图片上写字符串
            g2.drawString("科华控股股份有限公司", 55, 30);

            g2.setFont(new Font("黑体", Font.BOLD, 11));
            g2.drawString("固定资产", 127, 50);

            g2.setFont(new Font("微软雅黑", Font.PLAIN, 11));
            g2.drawString("机台名称：" + name, 138, 80);

            g2.setFont(new Font("微软雅黑", Font.PLAIN, 11));
//            g2.drawString("机台型号：" + type, 138, 105);

            g2.setFont(new Font("微软雅黑", Font.PLAIN, 11));
            g2.drawString("产线地址：" + productAddress, 138, 130);

            g2.setFont(new Font("微软雅黑", Font.PLAIN, 11));
            g2.drawString("资产编码：" + code, 138, 155);

            g2.setFont(new Font("黑体", Font.PLAIN, 11));
            // 图片上传后的路径
            ImageIO.write(bi, "png", new FileOutputStream(cardPath));
            return cardPath;
        } catch (Exception e) {
            throw new IllegalArgumentException("生成图片错误", e);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String qrPath = "d:/image/" + String.format("qr-%s.jpg", uuid);
            String resCode = "pv08-no." + (i + 1);
            String qrCode = "2repair:" + resCode;
            QrGen.encode(qrCode,
                    512, 512,
                    "D:\\work\\html\\logo.jpg",
                    qrPath);
            String path = GenerateImage.ImageBuilder("d:/image/" + String.format("card-%s.png", uuid),
                    qrPath,
                    resCode,
                    "工位机",
                    "pv08");
        }
//        System.out.println(path);
    }
}
